package Pattern.ObserverPattern.program2;

import java.util.ArrayList;

class Test2 {
    public static void main(String[] args) {
        SharesEvent se = new SharesEvent();
        SharesPeople s1 = new SharesPeople("杨锐");
        se.attach(s1);
        se.notifyPeople(6);
    }
}

abstract class Shares {
    ArrayList<SharesPeople> sharesPeople = new ArrayList<SharesPeople>();

    public void attach(SharesPeople people) {
        sharesPeople.add(people);
    }

    public void detach(SharesPeople people) {
        sharesPeople.remove(people);
    }

    public abstract void notifyPeople(int num);
}

class SharesEvent extends Shares {

    @Override
    public void notifyPeople(int num) {
        if (num >= 5){
            for (Object obj : sharesPeople){
                System.out.println("股票价格发生变化");
                ((SharesPeople)obj).response();
            }
        }else{
            System.out.println("股票变化正常");
        }
    }
}

interface SharesListener {
    void response();
}

class SharesPeople implements SharesListener {
    private String name;

    public SharesPeople(String name) {
        this.name = name;
    }

    @Override
    public void response() {
        System.out.println("正在发送通知股民成员："+name+"\r\n股票价格变化幅度达到5%..");
        System.out.println("***"+name+"已收到通知");
    }
}

